<?php

/**
 *    广告管理控制器
 *
 *    @author    bansen
 *    @usage    none
 */
class AdvApp extends BackendApp
{
    var $_adv_mod;

    function __construct()
    {
        $this->AdvApp();
    }

    function AdvApp()
    {
        parent::BackendApp();

        $this->_adv_mod =& m('banner');
    }

    /**
     *    管理
     *
     *    @author    Garbin
     *    @param    none
     *    @return    void
     */
    function index()
    {

		$banner_name  = isset($_GET['banner_name'])? ' banner_name LIKE "%'.trim($_GET['banner_name']).'%"' : '';
		$banner_catid = isset($_GET['banner_catid'])? ' and banner_catid='.intval($_GET['banner_catid']) : '';
		require(ROOT_PATH . '/api/adv.inc.php');
        $page   =   $this->_get_page(10);    //获取分页信息
        $advs = $this->_adv_mod->find(array(
            'conditions'    => $banner_name.$banner_catid,
            'limit'         => $page['limit'],  //获取当前页的数据
            'order'         => 'banner_sort,banner_time,banner_id desc',
            'count'         => true             //允许统计
        )); //找出所有商城的合作伙伴
		
        foreach ($advs as $key => $adv)
        {
            $adv['banner_pic']&&$advs[$key]['banner_pic'] = $adv['banner_pic'];
        }
        $page['item_count'] = $this->_adv_mod->getCount();   //获取统计的数据
        $this->_format_page($page);
        $this->import_resource(array('script' => 'inline_edit.js'));
		$this->assign('banner_catid' , $adv_data['banner_catid']);
		$this->assign('gets' , $_GET);
        $this->assign('filtered', $conditions? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);          //将分页信息传递给视图，用于形成分页条
		//echo'<pre>';
		//print_r($advs);exit;
        $this->assign('advs', $advs);
        $this->display('adv.index.html');
    }
    /**
     *    新增
     *
     *    @author    Garbin
     *    @return    void
     */
    function add()
    {
        if (!IS_POST)
        {
            /* 显示新增表单 */
			require(ROOT_PATH . '/api/adv.inc.php');
            $adv = array(
                'banner_sort'     => '255',
                'banner_url'      => 'http://',
                'banner_bg_color' =>'#FFFFFF',
            );
			$this->assign('banner_catid' , $adv_data['banner_catid']);
            $this->assign('adv' , $adv);
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('adv.form.html');
        }
        else
        {
            $data = array();
            $data['banner_name']      =   trim($_POST['banner_name']);
            $data['banner_catid']     =   intval($_POST['banner_catid']);
            $data['banner_location']  =   trim($_POST['banner_location']);
            $data['banner_url']       =   trim($_POST['banner_url']);
            $data['banner_sort']      =   intval($_POST['banner_sort']);
            $data['banner_bg_color']  =   trim($_POST['banner_bg_color']);
            $data['banner_status']    =   1;
            $data['banner_time']      =   time();

            if (!$adv_id = $this->_adv_mod->add($data))  //获取adv_id
            {
                $this->show_warning($this->_adv_mod->get_error());

                return;
            }
			//var_dump($adv_id);exit;
            /* 处理上传的图片 */
            $logo       =   $this->_upload_logo($adv_id);
            if ($logo === false)
            {
                return;
            }
            $logo && $this->_adv_mod->edit($adv_id, array('banner_pic' => $logo)); //将logo地址记下
            
            $this->_adv_mod->clear_template_cache();
            
            $this->show_message('add_partner_successed',
                'back_list',    'index.php?app=adv',
                'continue_add', 'index.php?app=adv&amp;act=add'
            );
        }
    }

    /**
     *    编辑
     *
     *    @author    Garbin
     *    @return    void
     */
    function edit()
    {	
		require(ROOT_PATH . '/api/adv.inc.php');
        $adv_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        if (!$adv_id)
        {
            $this->show_warning('no_such_adv');

            return;
        }
        if (!IS_POST)
        {
            $find_data     = $this->_adv_mod->find($adv_id);
            if (empty($find_data))
            {
                $this->show_warning('no_such_adv');

                return;
            }
            $adv    =   current($find_data);
            if ($adv['banner_pic'])
            {
                $adv['banner_pic']  =   $adv['banner_pic'];
            }
			$this->assign('banner_catid' , $adv_data['banner_catid']);
            $this->assign('adv', $adv);
			$this->assign('gets' , $_GET);
            $this->import_resource('jquery.plugins/jquery.validate.js');
            $this->display('adv.form.html');
        }
        else
        {
            $data = array();
            $data['banner_name']      =   trim($_POST['banner_name']);
            $data['banner_catid']     =   intval($_POST['banner_catid']);
            $data['banner_url']       =   trim($_POST['banner_url']);
            $data['banner_location']  =   trim($_POST['banner_location']);
            $data['banner_sort']      =   intval($_POST['banner_sort']);
            $data['banner_bg_color']  =   trim($_POST['banner_bg_color']);
            $logo               	  =   $this->_upload_logo($adv_id);
            $logo && $data['banner_pic'] = $logo;
            if ($logo === false)
            {
                return;
            }
            $rows = $this->_adv_mod->edit($adv_id, $data);
            if ($this->_adv_mod->has_error())    //有错误
            {
                $this->show_warning($this->_adv_mod->get_error());

                return;
            }

            $this->_adv_mod->clear_template_cache();
            
            $this->show_message('edit_partner_successed',
                'back_list',     'index.php?app=adv',
                'edit_again', 'index.php?app=adv&amp;act=edit&amp;id=' . $adv_id);
        }
    }

    //异步修改数据
   function ajax_col()  
   {
       $id     = empty($_GET['id']) ? 0 : intval($_GET['id']);
       $column = empty($_GET['column']) ? '' : trim($_GET['column']);
       $value  = isset($_GET['value']) ? trim($_GET['value']) : '';
       $data   = array();

       if (in_array($column ,array('title', 'banner_sort','banner_status')))
       {
           $data[$column] = $value;
           $this->_adv_mod->edit($id, $data);
           if(!$this->_adv_mod->has_error())
           {
               $this->_adv_mod->clear_template_cache();
               echo ecm_json_encode(true);
           }
       }
       else
       {
           return ;
       }
       return ;
   }

    function drop()
    {
        $adv_ids = isset($_GET['id']) ? trim($_GET['id']) : 0;
        if (!$adv_ids)
        {
            $this->show_warning('no_such_adv');

            return;
        }
        $adv_ids = explode(',', $adv_ids);//获取一个类似array(1, 2, 3)的数组
        if (!$this->_adv_mod->drop($adv_ids))    //删除
        {
            $this->show_warning($this->_adv_mod->get_error());

            return;
        }
        $this->_adv_mod->clear_template_cache();
        $this->show_message('drop_partner_successed');
    }

    /* 更新排序 */
    function update_order()
    {
        if (empty($_GET['id']))
        {
            $this->show_warning('Hacking Attempt');
            return;
        }

        $ids = explode(',', $_GET['id']);
        $sort_orders = explode(',', $_GET['banner_sort']);
        foreach ($ids as $key => $id)
        {
            $this->_adv_mod->edit($id, array('banner_sort' => $sort_orders[$key]));
        }
        $this->_adv_mod->clear_template_cache();
        $this->show_message('update_order_ok');
    }

    /**
     *    处理上传标志
     *
     *    @author    Garbin
     *    @param     int $adv_id
     *    @return    string
     */
    function _upload_logo($adv_id)
    {	
        $file = $_FILES['banner_pic'];
        if ($file['error'] == UPLOAD_ERR_NO_FILE) // 没有文件被上传
        {
            return '';
        }
        import('uploader.lib');             //导入上传类
        import('img_server');
        $uploader = new Uploader();
        $uploader->allowed_type(IMAGE_FILE_TYPE); //限制文件类型
        $uploader->addFile($_FILES['banner_pic']);//上传logo
		
        if (!$uploader->file_info())
        {
            $this->show_warning($uploader->get_error() , 'go_back', 'index.php?app=adv&amp;act=edit&amp;id=' . $adv_id);
            return false;
        }
        /* 指定保存位置的根目录 */
        $uploader->root_dir(ROOT_PATH);
        /* 上传 */
        if ($file_path = $uploader->save('data/files/mall/banner', $adv_id))   //保存到指定目录，并以指定文件名$adv_id存储
        {
            return get_image_server_url($file_path);
        }
        else
        {
            return false;
        }
    }
}

?>